package com.eii.excel.convert;

import cn.hutool.core.convert.Convert;
import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.eii.common.utils.JsonUtil;
import com.eii.excel.annotation.DictFormat;
import com.eii.excel.util.DictFormatUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.AnnotationUtils;

/**
 * Excel 数据字典转换器<br>
 * <strong>
 *   必要:此注解需要搭配 {@link DictFormat} 注解使用,以实现自定义字典值转换
 * </strong><br>
 */
public class DictConvert implements Converter<Object> {

    private static final Logger log = LoggerFactory.getLogger(DictConvert.class);

    @Override
    public Class<Object> supportJavaTypeKey() {
        return Object.class;
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.STRING;
    }

    @Override
    public Object convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty,
                                    GlobalConfiguration globalConfiguration) {
        // 使用字典解析
        String name = cellData.getStringValue();
        DictFormat dictFormatAttr = getDictFormat(contentProperty);
        String dictCode = DictFormatUtil.reverseByExp(name, dictFormatAttr.readConverterExp(), dictFormatAttr.separator());
        if (dictCode == null) {
            log.error("[convertToJavaData][DictFormat 解析失败 reverseByExp({})  name({})]", JsonUtil.toJSONString(dictFormatAttr), name);
            return null;
        }
        // 将 String 的 value 转换成对应的属性
        Class<?> fieldClazz = contentProperty.getField().getType();
        return Convert.convert(fieldClazz, dictCode);
    }

    @Override
    public WriteCellData<String> convertToExcelData(Object object, ExcelContentProperty contentProperty,
                                                    GlobalConfiguration globalConfiguration) {
        // 空时，返回空
        if (object == null) {
            return new WriteCellData<>("");
        }
        // 使用字典格式化
        String code = String.valueOf(object);
        DictFormat dictFormatAttr = getDictFormat(contentProperty);
        String dictData = DictFormatUtil.convertByExp(code, dictFormatAttr.readConverterExp(), dictFormatAttr.separator());
        if (dictData == null) {
            log.error("[convertToExcelData][DictFormat 转换失败 convertByExp({})  code({})]", JsonUtil.toJSONString(dictFormatAttr), code);
            return new WriteCellData<>("");
        }
        return new WriteCellData<>(dictData);
    }

    private static DictFormat getDictFormat(ExcelContentProperty contentProperty) {
        //因为@AliasFor是spring的特殊注解，不是Java原生支持的，因此要用Spring的工具类取值
        DictFormat annotation = AnnotationUtils.getAnnotation(contentProperty.getField(), DictFormat.class);
//        DictFormat annotation = contentProperty.getField().getAnnotation(DictFormat.class);
        return annotation;
    }
}
